﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder.Approval
{
    class B1Form_Approval : SAPBoAddon.B1Form
    {
        #region overload
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = "198";//消息处理
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        #endregion
        #region constraction
        public B1Form_Approval()
        {
            frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Approval_frm_ItemEvent);
        }

        void B1Form_Approval_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            { }
            else
            {
                if (pVal.ItemUID == "6" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_MATRIX_LINK_PRESSED)
                {
                    OpenApprovalForm(pVal.Row);
                }
            }
        }
        #endregion
        #region method
        private void OpenApprovalForm(int row)
        {
            SAPbouiCOM.Form oForm = CurrentForm;

            SAPbouiCOM.Matrix oMtx = oForm.Items.Item("5").Specific;
            int SelectedRow = oMtx.GetNextSelectedRow();
            if (oMtx.GetCellSpecific("1", SelectedRow).Value == "单据审批")
            {
                SAPbouiCOM.Matrix oMtx1 = oForm.Items.Item("6").Specific;
                string refValue = oMtx1.GetCellSpecific(1, row).Value;
                string ObjectKey = refValue.ToString();
                SAPbouiCOM.Form oChildForm = SAPBoAddon.B1Assistant.Form.LoadToSBOApplciation(System.Windows.Forms.Application.StartupPath + @"\Form\AVA_ApprovalStepForm.srf", "", true);
                oChildForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                SAPBoAddon.B1Assistant.Item.SetEnabled(oChildForm, "35", true);
                SAPBoAddon.B1Assistant.Item.SetValue(oChildForm, "35", ObjectKey);
                SAPBoAddon.B1Assistant.Item.SetEnabled(oChildForm, "23", false);
                SAPBoAddon.B1Assistant.Item.SetEnabled(oChildForm, "40", true);
                SAPBoAddon.B1Assistant.Item.SetEnabled(oChildForm, "41", true);
                SAPBoAddon.B1Assistant.Item.SetEnabled(oChildForm, "27", true);
                oChildForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
            }
            else
            {
                SAPbouiCOM.Matrix oMtx1 = oForm.Items.Item("6").Specific;
                IEnumerator enumerator = oMtx1.Columns.GetEnumerator();
                int index = 0;
                while (enumerator.MoveNext())
                {
                    SAPbouiCOM.Column column = enumerator.Current as SAPbouiCOM.Column;
                    if (column.TitleObject.Caption == "单据类型")
                    {
                        SAPBoAddon.B1Assistant.Matrix.GetCellValue(oMtx1, column.UniqueID, row);
                        string objName = oMtx1.GetCellSpecific(index, row).Value;
                        string ObjectKey = oMtx1.GetCellSpecific(1, row).Value.ToString();
                        Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                        rs.DoQuery(String.Format("select Code from OUDO where Name = '{0}'", objName));
                        if (rs.RecordCount > 0)
                        {
                            string objType = rs.Fields.Item("Code").Value;

                            //SAPbouiCOM.Form oChildForm = SAPBoAddon.B1Assistant.Form.LoadToSBOApplciation(System.Windows.Forms.Application.StartupPath + @"\Form\" + objType + @"Form.srf", "");
                            //oChildForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                            //SAPBoAddon.B1Assistant.Item.SetValue(oChildForm, oChildForm.DataBrowser.BrowseBy, ObjectKey);
                            //oChildForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);

                            this.ShowForm(objType, ObjectKey);
                        }
                        break;
                    }
                    index++;
                }

            }
        }
        #endregion
    }
}
